# MCP天气预警系统测试脚本
# 测试McpApi的天气预警和MES集成功能

param(
    [string]$BaseUrl = "http://localhost:5002",
    [string]$City = "Beijing"
)

Write-Host "=== MCP天气预警系统测试 ===" -ForegroundColor Green
Write-Host "服务地址: $BaseUrl" -ForegroundColor Yellow
Write-Host "测试城市: $City" -ForegroundColor Yellow
Write-Host ""

# 测试函数
function Test-MCPEndpoint {
    param(
        [string]$Url,
        [string]$Method = "GET",
        [object]$Body = $null,
        [string]$Description
    )
    
    Write-Host "测试: $Description" -ForegroundColor Cyan
    
    try {
        $headers = @{
            "Content-Type" = "application/json"
        }
        
        if ($Method -eq "POST" -and $Body) {
            $jsonBody = $Body | ConvertTo-Json -Depth 10
            Write-Host "请求体: $jsonBody" -ForegroundColor Gray
            $response = Invoke-RestMethod -Uri $Url -Method $Method -Body $jsonBody -Headers $headers
        } else {
            $response = Invoke-RestMethod -Uri $Url -Method $Method -Headers $headers
        }
        
        Write-Host "✅ 成功" -ForegroundColor Green
        Write-Host "响应: $($response | ConvertTo-Json -Depth 3)" -ForegroundColor White
        return $true
    }
    catch {
        Write-Host "❌ 失败: $($_.Exception.Message)" -ForegroundColor Red
        return $false
    }
    
    Write-Host ""
}

function Test-MCPProtocol {
    param(
        [string]$Method,
        [object]$Params = @{},
        [string]$Description
    )
    
    $mcpRequest = @{
        jsonrpc = "2.0"
        id = Get-Random
        method = $Method
        params = $Params
    }
    
    return Test-MCPEndpoint -Url "$BaseUrl/api/mcp" -Method "POST" -Body $mcpRequest -Description $Description
}

# 1. 基础健康检查
Write-Host "1. 基础服务检查" -ForegroundColor Magenta
Test-MCPEndpoint -Url "$BaseUrl/api/mcp/health" -Description "健康检查"
Test-MCPEndpoint -Url "$BaseUrl/api/mcp/info" -Description "服务信息"

# 2. MCP协议测试
Write-Host "2. MCP协议测试" -ForegroundColor Magenta

# 初始化
$initParams = @{
    protocolVersion = "2024-11-05"
    capabilities = @{
        tools = @{
            listChanged = $false
        }
    }
    clientInfo = @{
        name = "Test Client"
        version = "1.0.0"
    }
}
Test-MCPProtocol -Method "initialize" -Params $initParams -Description "MCP初始化"

# 获取工具列表
Test-MCPProtocol -Method "tools/list" -Params @{} -Description "获取工具列表"

# 3. 天气工具测试
Write-Host "3. 天气工具测试" -ForegroundColor Magenta

# 获取当前天气
$weatherParams = @{
    name = "get_current_weather"
    arguments = @{
        city = $City
        countryCode = "CN"
    }
}
Test-MCPProtocol -Method "tools/call" -Params $weatherParams -Description "获取当前天气"

# 获取天气预报
$forecastParams = @{
    name = "get_weather_forecast"
    arguments = @{
        city = $City
        days = "3"
    }
}
Test-MCPProtocol -Method "tools/call" -Params $forecastParams -Description "获取天气预报"

# 检查天气预警
$alertParams = @{
    name = "check_weather_alerts"
    arguments = @{
        city = $City
    }
}
Test-MCPProtocol -Method "tools/call" -Params $alertParams -Description "检查天气预警"

# 4. 天气影响分析测试
Write-Host "4. 天气影响分析测试" -ForegroundColor Magenta

# 综合影响分析
$impactParams = @{
    name = "analyze_weather_impact"
    arguments = @{
        city = $City
        analysisType = "comprehensive"
    }
}
Test-MCPProtocol -Method "tools/call" -Params $impactParams -Description "天气影响分析"

# 生成预警报告
$reportParams = @{
    name = "generate_weather_alert_report"
    arguments = @{
        city = $City
        severity = "all"
    }
}
Test-MCPProtocol -Method "tools/call" -Params $reportParams -Description "生成预警报告"

# 获取适应性建议
$adviceParams = @{
    name = "get_weather_adaptation_advice"
    arguments = @{
        city = $City
        timeHorizon = "24h"
    }
}
Test-MCPProtocol -Method "tools/call" -Params $adviceParams -Description "获取适应性建议"

# 5. MES工具测试
Write-Host "5. MES工具测试" -ForegroundColor Magenta

# 查询生产订单
$ordersParams = @{
    name = "query_production_orders"
    arguments = @{
        status = "进行中"
        pageSize = "5"
    }
}
Test-MCPProtocol -Method "tools/call" -Params $ordersParams -Description "查询生产订单"

# 查询物料库存
$inventoryParams = @{
    name = "query_material_inventory"
    arguments = @{
        materialCode = "MAT001"
    }
}
Test-MCPProtocol -Method "tools/call" -Params $inventoryParams -Description "查询物料库存"

# 查询BOM信息
$bomParams = @{
    name = "query_bom_info"
    arguments = @{
        productCode = "PRD001"
    }
}
Test-MCPProtocol -Method "tools/call" -Params $bomParams -Description "查询BOM信息"

# 生成生产报表
$productionReportParams = @{
    name = "generate_production_report"
    arguments = @{
        reportType = "daily"
        startDate = (Get-Date).ToString("yyyy-MM-dd")
    }
}
Test-MCPProtocol -Method "tools/call" -Params $productionReportParams -Description "生成生产报表"

# 6. 综合场景测试
Write-Host "6. 综合场景测试" -ForegroundColor Magenta

Write-Host "场景: 恶劣天气下的生产影响评估" -ForegroundColor Yellow

# 模拟恶劣天气场景
Write-Host "步骤1: 检查天气状况" -ForegroundColor Gray
Test-MCPProtocol -Method "tools/call" -Params $weatherParams -Description "获取当前天气"

Write-Host "步骤2: 分析对生产的影响" -ForegroundColor Gray  
Test-MCPProtocol -Method "tools/call" -Params $impactParams -Description "分析天气影响"

Write-Host "步骤3: 获取当前生产状态" -ForegroundColor Gray
Test-MCPProtocol -Method "tools/call" -Params $ordersParams -Description "查询生产订单"

Write-Host "步骤4: 生成应对建议" -ForegroundColor Gray
Test-MCPProtocol -Method "tools/call" -Params $adviceParams -Description "获取适应性建议"

# 7. 性能测试
Write-Host "7. 性能测试" -ForegroundColor Magenta

$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()

# 并发调用多个工具
$jobs = @()
$jobs += Start-Job -ScriptBlock {
    param($BaseUrl, $City)
    $weatherParams = @{
        name = "get_current_weather"
        arguments = @{
            city = $City
        }
    }
    $mcpRequest = @{
        jsonrpc = "2.0"
        id = 1
        method = "tools/call"
        params = $weatherParams
    }
    Invoke-RestMethod -Uri "$BaseUrl/api/mcp" -Method "POST" -Body ($mcpRequest | ConvertTo-Json -Depth 10) -ContentType "application/json"
} -ArgumentList $BaseUrl, $City

$jobs += Start-Job -ScriptBlock {
    param($BaseUrl)
    $ordersParams = @{
        name = "query_production_orders"
        arguments = @{
            pageSize = "5"
        }
    }
    $mcpRequest = @{
        jsonrpc = "2.0"
        id = 2
        method = "tools/call"
        params = $ordersParams
    }
    Invoke-RestMethod -Uri "$BaseUrl/api/mcp" -Method "POST" -Body ($mcpRequest | ConvertTo-Json -Depth 10) -ContentType "application/json"
} -ArgumentList $BaseUrl

# 等待所有任务完成
$jobs | Wait-Job | Out-Null
$stopwatch.Stop()

Write-Host "并发测试完成，耗时: $($stopwatch.ElapsedMilliseconds)ms" -ForegroundColor Green

# 清理任务
$jobs | Remove-Job

# 8. 总结
Write-Host "8. 测试总结" -ForegroundColor Magenta

Write-Host "✅ MCP天气预警系统测试完成" -ForegroundColor Green
Write-Host "📊 主要功能:" -ForegroundColor Yellow
Write-Host "   - 天气数据获取和预警" -ForegroundColor White
Write-Host "   - MES系统数据查询" -ForegroundColor White  
Write-Host "   - 天气影响分析" -ForegroundColor White
Write-Host "   - 智能预警和建议" -ForegroundColor White
Write-Host ""
Write-Host "🔗 扣子AI集成地址: $BaseUrl/api/mcp" -ForegroundColor Cyan
Write-Host "📖 配置指南: McpApi/COZE_AI_SETUP_GUIDE.md" -ForegroundColor Cyan
Write-Host ""
Write-Host "系统已准备就绪，可以配置扣子AI智能体进行使用！" -ForegroundColor Green
